package com.grao.waterschool.human.manager.service.impl;

import cn.hutool.core.collection.CollectionUtil;

import com.grao.waterschool.human.common.LogonType;
import com.grao.waterschool.human.common.PageResult;
import com.grao.waterschool.human.entiry.LogParam;
import com.grao.waterschool.human.entiry.po.LogOperation;
import com.grao.waterschool.human.entiry.po.Logon;
import com.grao.waterschool.human.manager.service.LogService;
import com.grao.waterschool.human.mapper.LogOperationMapper;
import com.grao.waterschool.human.mapper.LogonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private LogonMapper logonMapper;
    @Autowired
    private LogOperationMapper logOperationMapper;

    @Override
    public void logon(String username, String device, String clientIp) {
        Logon log = new Logon();
        log.setUsername(username);
        log.setType(LogonType.LOGIN.getValue());
        log.setDevice(device);
        log.setIp(clientIp);
        logonMapper.insert(log);
    }

    @Override
    public void logOperation(LogOperation log) {
        logOperationMapper.insert(log);
    }

    @Override
    public PageResult<Logon> selectLogonListPage(LogParam param) {
        List<Logon> logs = logonMapper.selectLogonListPage(param);
        if (CollectionUtil.isEmpty(logs)) {
            return new PageResult<>();
        }

        long total = logonMapper.selectLogonListCount(param);
        return new PageResult<>(total, logs);
    }

    @Override
    public PageResult<LogOperation> selectLogOperationListPage(LogParam param) {
        List<LogOperation> logs = logOperationMapper.selectLogOperationListPage(param);
        if (CollectionUtil.isEmpty(logs)) {
            return new PageResult<>();
        }

        long total = logOperationMapper.selectLogOperationListCount(param);
        return new PageResult<>(total, logs);
    }
}
